let db = require("../db/index");

// 查询住户列表
exports.householdlist = (req, res) => {
    var sql = `select * from housing,household WHERE housing.date=household.date`;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }else{
            console.log(...data)
            let arr=[]
            data.forEach(item=>{
                arr.push({
                        ...item,
                        PropertyName:item.Belongingcommunity+item.Buildingname+item.Numberofunits+'单元'+item.RoomNo
                    }
                )
            })
            res.send({
                arr
            });
        }
        
    });
};
exports.householdselect = (req, res) => {
    // console.log(req.body)
    var sql = `SELECT * FROM household where date=${req.body.date}`;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }else{
            res.send({
                data,
                state:200
            });
        }
        
    });
};


// 添加住户(无则添加，有则修改)
exports.householdinsert = (req, res) => {
    req.body.domains.forEach((item,index)=>{
        if(item.household_id){
            console.log('修改')
            var sql = `UPDATE household SET householdName='${item.householdName}' ,householdPhone='${item.householdPhone}',householeIDcard='${item.householeIDcard}',householdType='${item.householdType}' WHERE household_id=${item.household_id}`;
            db.query(sql, [req.body.id], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err);
                }
            });
        }else{
            console.log('添加')
            var sql = `INSERT INTO household(nickname,householdName,householdPhone,householeIDcard,householdType,householdState,examine,del,date)VALUES('一缕青烟','${item.householdName}','${item.householdPhone}','${item.householeIDcard}','${item.householdType}','待审核','审核','删除',${req.body.date})`;
            db.query(sql, [req.body.id], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err);
                }
            });
        }
    })
    res.send({
        state:200
    })
};
// 删除用户
exports.householddelete = (req, res) => {
    // console.log(req.body)
    var sql = `DELETE FROM household WHERE household_id=${req.body.id}`;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }else{
            // res.send({
            //     data,
            //     state:200
            // });
            var sql = `select * from housing,household WHERE housing.date=household.date`;
            db.query(sql, [req.body.id], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err);
                }else{
                    console.log(...data)
                    let arr=[]
                    data.forEach(item=>{
                        arr.push({
                                ...item,
                                PropertyName:item.Belongingcommunity+item.Buildingname+item.Numberofunits+'单元'+item.RoomNo
                            }
                        )
                    })
                    res.send({
                        arr
                    });
                }
                
            });
        }
        
    });
};

// 修改用户状态
exports.householdupdate = (req, res) => {
    var sql = `UPDATE  household SET householdState='${req.body.resource=="审核通过"?'已审核':'已拒绝'}' ,examine='详情',accesscontol='${req.body.accesscontol}',refaulcase='${req.body.refaulcase}' WHERE household_id=${req.body.household_id}`;
    db.query(sql, [req.body.id], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err);
        }else{
            var sql = `select * from housing,household WHERE housing.date=household.date`;
            db.query(sql, [req.body.id], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err);
                }else{
                    console.log(...data)
                    let arr=[]
                    data.forEach(item=>{
                        arr.push({
                                ...item,
                                PropertyName:item.Belongingcommunity+item.Buildingname+item.Numberofunits+'单元'+item.RoomNo
                            }
                        )
                    })
                    res.send({
                        arr
                    });
                }
                
            });
        }
        
    });
};

